package com.online.onlinestudent.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.online.onlinestudent.dto.AjaxResult;
import com.online.onlinestudent.entity.Admin;
import com.online.onlinestudent.entity.Student;
import com.online.onlinestudent.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admins")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @GetMapping
    public AjaxResult getAllAdmins() {
        List<Admin> admins = adminService.list();
        if (admins == null || admins.isEmpty()) {
            return AjaxResult.fail("未查询到结果！");
        }
        return AjaxResult.success(admins);
    }

    @GetMapping("/{id}")
    public AjaxResult getAdminById(@PathVariable Long id) {
        Admin admin = adminService.getById(id);
        if (admin != null) {
            return AjaxResult.success(admin);
        } else {
            return AjaxResult.fail("不存在该管理员！");
        }
    }

    @PostMapping
    public AjaxResult saveAdmin(@RequestBody Admin admin) {
        try {
            boolean saved = adminService.save(admin);
            if (saved) {
                return AjaxResult.success("插入成功！");
            } else {
                return AjaxResult.fail("插入失败！");
            }
        } catch (Exception e) {
            return AjaxResult.fail("插入失败，已存在该管理员！" );
        }
    }

    @PutMapping("/{id}")
    public AjaxResult updateAdmin(@PathVariable Long id, @RequestBody Admin admin) {
        admin.setId(id);
        try {
            boolean updated = adminService.updateById(admin);
            if (updated) {
                return AjaxResult.success("更新成功！");
            } else {
                return AjaxResult.fail("更新失败，请重新输入！");
            }
        } catch (Exception e) {
            return AjaxResult.fail("更新失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteAdminById(@PathVariable Long id) {
        try {
            boolean deleted = adminService.removeById(id);
            if (deleted) {
                return AjaxResult.success("删除成功！");
            } else {
                return AjaxResult.fail("删除失败，不存在该管理员！");
            }
        } catch (Exception e) {
            return AjaxResult.fail("删除失败：" + e.getMessage());
        }
    }

    @GetMapping("/page")
    public AjaxResult getAdminsByPage(@RequestParam int current, @RequestParam int size) {
        try {
            Page<Admin> page = adminService.page(new Page<>(current, size));
            if (page == null || page.getRecords().isEmpty()) {
                return AjaxResult.fail("未查询到结果！");
            }
            return AjaxResult.success(page);
        } catch (Exception e) {
            return AjaxResult.fail("查询失败：" + e.getMessage());
        }
    }

    @PostMapping("/username")
    public AjaxResult fuzzyQueryByUsername(@RequestBody Admin admin) {
        String username = admin.getUsername();
        if (username == null || username.trim().isEmpty()) {
            return AjaxResult.fail("用户名不能为空！");
        }
        try {
            List<Admin> list = adminService.fuzzyQueryByUsername(username);
            if (list.isEmpty()) {
                return AjaxResult.fail("未查询到结果！");
            }
            return AjaxResult.success(list);
        } catch (Exception e) {
            return AjaxResult.fail("查询失败：" + e.getMessage());
        }
    }
}
